// pages/lock/lock.js
import weapp from '../../components/weapp/index';

var encryp = require('../../assets/js/encryp.js')
var wxRequest = require('../../assets/js/wxRequest.js')
var common = require('../../assets/js/common.js')
var base64 = require('../../assets/js/base64.js')
var base = require('../../assets/js/base.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 1,      //当前页
    token: '',
    randomKey: '',
    sign: '',
    userId: '',
    lockArray: [],  //钥匙列表
    keySearch: [],
    keyInfo: [],

    //---------search
    inputShowed: false,
    inputVal: "",
    searchFlag: false,  //是否显示出搜索结果

    showSearchAndPage: false,
    showLoadMore: false,  //是否显示加载更多动画
    modalInfo: true,

    icons: base64.key_keyIcon,
  },
  /**
   * 根据当前用户获取钥匙
   */
  getkeyByUser: function (userId, offset){
    var that = this;
    var simpleObject = {
      id: userId,
      offset: offset,
      limit: getApp().globalData.limit
    };
    var param = common.packParam(that.data.randomKey, simpleObject);
    var url = getApp().globalData.httpUrl + '/key/getKeyByUser';
    var info = wxRequest.postRequest(url, param, that.data.token).
      then(res => {
        console.log(res.data)
        if (res.data == '' || res.data == []) {
          that.setData({
            lockArray: res.data
          })
          this.oLoader.setStatus('empty');
        } else {
          that.setData({
            lockArray: res.data,
            showSearchAndPage: true,  //显示搜索框和页码
            showLoadMore: false   //隐藏加载更多动画
          })
          this.oLoader.setStatus('');
        }
        
      })
  },
  /**
   * 搜索钥匙
   */
  searchKey: function(keyName, userId){
    var that = this;
    var simpleObject = {
      id: userId,
      keyName: base.utf16to8(keyName)
    };
    var param = common.packParam(that.data.randomKey, simpleObject);
    var url = getApp().globalData.httpUrl + '/key/KeySearch';
    var info = wxRequest.postRequest(url, param, that.data.token).
      then(res => {
        console.log(res.data)
        if (res.data == '' || res.data == []) {
          that.setData({
            keySearch: res.data,
            lockArray: []
          })
          this.oLoader.setStatus('empty');
        } else {
          that.setData({
            keySearch: res.data,
          })
          this.oLoader.setStatus('');
        }

      })
  },
  //确认    
  confirm: function () {
    var that = this;
    this.setData({ modalInfo: !that.data.modalInfo })
  },
  keyInfo: function (e) {
    var that = this;
    this.setData({ modalInfo: !that.data.modalInfo })
    var index = parseInt(e.currentTarget.dataset.index)

    let keyInfo = that.data.lockArray[index];
    console.log(keyInfo)
    that.setData({
      keyInfo: keyInfo
    })
  },
  /**
   * 上一页
   */
  preView: function () {
    var that = this;
    if (that.data.currentPage <= 1) {
      return;
    }
    //页数减1
    var page = that.data.currentPage - 1;
    that.setData({
      currentPage: page
    })
    that.getkeyByUser(that.data.userId, that.data.currentPage);
  },
  /**
   * 下一页
   */
  nextView: function () {
    var that = this;
    that.setData({
      showLoadMore: true   //显示加载更多动画
    })

    //页数加1
    var page = that.data.currentPage + 1;
    that.setData({
      currentPage: page
    })
    that.getkeyByUser(that.data.userId, that.data.currentPage);
  },
  // ------------------搜索begin---------------
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  // inputTyping: function (e) {
  //   var that = this;
  //   this.setData({
  //     inputVal: e.detail.value
  //   });
  //   console.log(e.detail.value)
  //   // 搜索钥匙
  //   var arr = that.data.lockArray;  //全部钥匙的列表
  //   var arrInput = [];          //符合结果的列表

  //   for (var i = 0; i < arr.length; i++) {               
  //     if ((arr[i].keyName).match(e.detail.value + ".*") != null) {
  //       arrInput.push(arr[i])
  //     }
  //   }
  //   this.setData({
  //     keySearch: arrInput

  //   });
  // },
  inputConfirm: function(e){
    var that = this;
    that.setData({
      inputVal: e.detail.value
    });
    that.searchKey(that.data.inputVal, that.data.userId)
  },
  /**
   * 点击搜索结果
   */
  search_click: function (e) {
    var that = this;
    var index = parseInt(e.currentTarget.dataset.index)
    var searchItem = that.data.keySearch[index]
    
    that.setData({
      lockArray: [],
      searchFlag: true
    })
    var data = {
      lockArray: that.data.lockArray
    };
    data.lockArray.push(searchItem)
    this.setData(data)
    that.hideInput()

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 每个页面都需要先获取缓存中的token和security
    var tokens = wx.getStorageSync('token')
    var randomKey = wx.getStorageSync('randomKey')
    var sign = wx.getStorageSync('sign')
    var id = wx.getStorageSync('id')
    if (tokens && randomKey) {
      that.setData({
        token: tokens,
        randomKey: randomKey,
        sign: sign,
        userId: id
      })
    }
    that.getkeyByUser(that.data.userId,that.data.currentPage);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    this.oLoader = weapp.Loader();
    var lockArray = common.isBlank(that.data.lockArray)
    if (lockArray) {
      this.oLoader.setStatus('empty');
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})